package com.apache.member.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.member.entity.MemberRole;
import com.apache.member.manager.MemberRoleManager;

public class MemberRoleManagerImpl implements MemberRoleManager {

	protected IDao memberRoleDao;
	protected final String entityName = "com.apache.member.entity.MemberRole";

	public void setMemberRoleDao(IDao memberRoleDao) {
		this.memberRoleDao = memberRoleDao;
	}

	public String saveInfo(ParamsVo<MemberRole> vo) throws BusinessException {
		MemberRole memberRole = vo.getObj();

		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="memberRole_"+infoId;
		memberRole.setRoleId(infoId);
		MethodParam param = new MethodParam("MemberRole", cacheKey, "", entityName);
		param.setVaule(memberRole);
		if (memberRoleDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<MemberRole> vo) throws BusinessException {
		MemberRole memberRole = vo.getObj();
		if (Validator.isNotNull(memberRole.getRoleId())) {
			String cacheKey = "";
			//String cacheKey="memberRole_"+memberRole.getRoleId();
			MethodParam param = new MethodParam("MemberRole", cacheKey, "", entityName);
			param.setVaule(memberRole);
			return memberRoleDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<MemberRole> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="memberRole_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		MemberRole info = (MemberRole) memberRoleDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("MemberRole");
			return memberRoleDao.edit(param);
		} else {
			param.setParams("roleId", infoId);
			param.setDelete(true);
			return memberRoleDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<MemberRole> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="memberRole_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return memberRoleDao.selectById(param);
	}

	public Object execute(ParamsVo<MemberRole> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<MemberRole> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = memberRoleDao.pageSelect(param);
		return page;
	}

	public List<MemberRole> getList(ParamsVo<MemberRole> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return memberRoleDao.select(param);
	}

	public long countInfo(ParamsVo<MemberRole> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return memberRoleDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<MemberRole> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(memberRoleDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		MemberRole memberRole = vo.getObj();
		if (Validator.isNotNull(memberRole.getMemberId())) {
			sb.append(" and memberId = :memberId");
			param.setParams("memberId", memberRole.getMemberId());
		}
		if (Validator.isNotNull(memberRole.getMemberEname())) {
			sb.append(" and memberEname = :memberEname");
			param.setParams("memberEname", memberRole.getMemberEname());
		}
		if (Validator.isNotNull(memberRole.getMemberCname())) {
			sb.append(" and memberCname = :memberCname");
			param.setParams("memberCname", memberRole.getMemberCname());
		}
		if (Validator.isNotNull(memberRole.getRoleEname())) {
			sb.append(" and roleEname = :roleEname");
			param.setParams("roleEname", memberRole.getRoleEname());
		}
		if (Validator.isNotNull(memberRole.getRoleCname())) {
			sb.append(" and roleCname = :roleCname");
			param.setParams("roleCname", memberRole.getRoleCname());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
